const {mysql} = require('../qcloud')

module.exports = async (ctx)=>{
  const {page, openid} = ctx.request.query
  const size = 10 //每页显示的条数
  const mysqlSelect =  mysql('books')
                      .select('books.*','cSessionInfo.user_info')
                      .join('cSessionInfo','books.openid','cSessionInfo.open_id')
                      .orderBy('books.id','desc')
  let books

  if(openid){
    //我上传的图书列表
    books = await mysqlSelect.where('books.openid',openid)
    console.log('books',books)
  }else{
    //全部图书
    books = await mysqlSelect.limit(size).offset(Number(page) * size)
  }

  ctx.state.data = {
    list:books.map(v=>{
      const info = JSON.parse(v.user_info)
      return Object.assign({},v,{
        user_info:{
          nickName:info.nickName,
          user_img:info.avatarUrl
        }
      }) 
    })
  }
}